package edu.qichen.onlineBlog.framework.exception;

import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.Serial;
import java.util.function.Supplier;

/**
 * 功能:
 * 作者:程序员欧阳子韩
 * 日期:2024/4/6 18:51
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class BusinessException extends RuntimeException {
    @Serial
    private static final long serialVersionUID = -2623856311970899169L;

    private int errCode = ErrorCodeEnum.COMMON_ERROR.getErrCode();

    private String errMsg;

    private BusinessException(int _errCode, String _errMsg) {
        super(_errMsg);
        this.errCode = _errCode;
        this.errMsg = _errMsg;
    }

    private BusinessException(String _errMsg) {
        super(_errMsg);
        this.errMsg = _errMsg;
    }

    private BusinessException(Throwable _throwable) {
        super(_throwable);
        this.errMsg = _throwable.getMessage();
    }

    private BusinessException(Throwable _throwable, String _errMsg) {
        super(_errMsg, _throwable);
        this.errMsg = _throwable.getMessage();
    }

    public BusinessException(ErrorCodeEnum errorCodeEnum, String remarks) {
        super(errorCodeEnum.getErrMsg() + ":" + remarks);
        this.errCode = errorCodeEnum.getErrCode();
        this.errMsg = errorCodeEnum.getErrMsg() + ":" + remarks;
    }

    public BusinessException(ErrorCodeEnum errorCodeEnum, Throwable cause) {
        super(errorCodeEnum.getErrMsg(), cause);
        this.errCode = errorCodeEnum.getErrCode();
        this.errMsg = errorCodeEnum.getErrMsg();
    }

    public BusinessException(ErrorCodeEnum errorCodeEnum, String remarks, Throwable cause) {
        super(errorCodeEnum.getErrMsg() + ":" + remarks, cause);
        this.errCode = errorCodeEnum.getErrCode();
        this.errMsg = errorCodeEnum.getErrMsg() + ":" + remarks;
    }

    public static BusinessException build(String errMsg) {
        return new BusinessException(errMsg);
    }


    public static BusinessException build(ErrorCodeEnum errorCodeEnum) {
        return new BusinessException(errorCodeEnum.getErrCode(), errorCodeEnum.getErrMsg());
    }

    public static BusinessException build(ErrorCodeEnum errorCodeEnum, String remarks) {
        return new BusinessException(errorCodeEnum.getErrCode(), errorCodeEnum.getErrMsg() + ":" + remarks);
    }

    public static BusinessException build(Throwable throwable) {
        return new BusinessException(throwable);
    }

    public static BusinessException build(String errMsg, Object... objs) {
        if (objs != null) {
            for (int i = 0; i < objs.length; i++) {
                errMsg = errMsg.replaceFirst("\\{\\}", String.valueOf(objs[i]));
            }
        }
        return build(errMsg);
    }

    public static BusinessException build(Throwable throwable, String errMsg, Object... objs) {
        if (objs != null) {
            for (int i = 0; i < objs.length; i++) {
                errMsg = errMsg.replaceFirst("\\{\\}", String.valueOf(objs[i]));
            }
        }

        return new BusinessException(throwable, errMsg);
    }

    public static void checkException(String msg, Supplier<Boolean> checkSupplier) {
        if (checkSupplier.get()) {
            throw BusinessException.build(msg);
        }
    }


}
